package com.hosever.coreTest;

import java.util.Arrays;



public class Test20160517{
	
	public static void main(String[] args) {
		//统计二进制1的个数
		//int i = 0xFF;
		//System.out.println(bits(i));
		
		//模拟integer的字符串转数字
		//String s = "-32";
		//Integer.valueOf(s);
		//System.out.println(String2Integer(s));
		
		//合并排序
//		int[] a = {1,5};
//		int[] b = {2,6};
//		int[] result = mergeSorted(a, b);
//		System.out.println(Arrays.toString(result));
		
		//去重
//		int[] array = {1,2,2,3,3,3};
//		System.out.println(unique(array));
//		System.out.println(Arrays.toString(array));
		
		
		//多线程交替
		final Test20160517 t = new Test20160517();
		//线程1
		new Thread(new Runnable() {
			@Override
			public void run() {
				int i = 1;
				while(i<=100){
					synchronized (t) {
						System.out.print(i);
						i+=2;
						System.out.print("...线程1唤醒所有线程");
						t.notifyAll();
						try {
							System.out.println("...线程1阻塞");
							t.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
		//线程2
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				int j = 2;
				while(j<=100){
					synchronized (t) {
						System.out.print(j);
						j+=2;
						System.out.print("...线程2唤醒所有线程");
						t.notifyAll();
						try {
							System.out.println("...线程2阻塞");
							t.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
		
	}
	
	//统计二进制1的个数
	public static int bits(int i){
		int result = 0;
		System.out.println(i);
		String a = Integer.toBinaryString(i);
		System.out.println(a);
		char[] aCharArray = a.toCharArray();
		for (char c : aCharArray) {
			if(c=='1'){
				result++;
			}
		}
		
		return result;
	}
	
	//模拟integer的字符串转数字
	public static int String2Integer(String str){
		int result = 0;
		int i = 0;
		int len = str.length();
		int digit = 0;
		int radix = 10;//进制
		boolean negative = false;//是否是负数
		
		if(len>0){
			char firstChar = str.charAt(0);
			if (firstChar=='-') {
				negative=true;
			}
		}
		while (i < len) {
            digit = Character.digit(str.charAt(i++),radix);
            if (digit < 0) {//负号转化成-1
            	continue;
            }
            result *= radix;
            System.out.println(result+" "+digit);
            result += digit;
        }
		System.out.println(negative?-result:result);
		return result;
	}
	/**
	 * 合并排序
	 * @param arr1
	 * @param arr2
	 * @return
	 */
	public static int[] mergeSorted(int[] arr1,int[] arr2){
		int[] result = new int[arr1.length+arr2.length];
		System.arraycopy(arr1, 0, result, 0, arr1.length);  
		System.arraycopy(arr2, 0, result, arr1.length, arr2.length);  
		Arrays.sort(result, 0, result.length);
		return result;
	}
	
	/**
	 * 去重（空间复杂度为O(1):只用一个数组）
	 * @param array 传入已排序存在重复数字的整数数组，执行后前N个是去重后的数组
	 * @return 去重后数组长度
	 */
	public static int unique(int[] array){
		int[] resultArray = new int[array.length];
		int index = 0;
		for (int i : array) {
			boolean isExist = false;
			for (int j : resultArray) {
				if(i==j){
					isExist = true;
				}
			}
			if(!isExist){
				resultArray[index++]=i;
			}
		}
		
		for(int i=0 ; i<array.length ; i++){
			array[i] = resultArray[i];
		}
		
		return index;
	}
	
	
}


